Skip to main content

Word Counter

Say you have a <textarea> and you want to retrieve info about the number of:

 Characters (total)
Characters (no spaces)
Words
Lines
function wordCount( val ){
var wom = val.match(/\S+/g);
return {
charactersNoSpaces : val.replace(/\s+/g, '').length,
characters : val.length,
words : wom ? wom.length : 0,
lines : val.split(/\r*\n/).length
};
}


// Use like:
wordCount( someMultilineText ).words; // (Number of words)